package com.wangan.wanganpush.services;


import com.wangan.wanganpush.props.MyProps;
import com.wangan.wanganpush.utils.FtpUtils;
import com.wangan.wanganpush.utils.StringToXml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 拼接文件发送文件流
 */
@Service
public class GetXmlSendZipUtil {
    @Autowired
    StringToXml stringToXml;

    @Resource
    MyProps myProps;
   public  synchronized  void  sendZip(String serviceType, String xmlStr,String messageCode,String fileName,String zipName1){
      try {
          String zipName = "";
          if(zipName1 == null){
              zipName = stringToXml.stringToXml(xmlStr,fileName);
          }else{
              zipName = zipName1;
          }
         if(zipName == null) return ;
          //调用网安推送方法
          try {
          FileInputStream in=new FileInputStream(new File(myProps.getJarPath()+zipName));
          Boolean flag= FtpUtils.uploadFile(myProps.getWangAnIp(),myProps.getFtpUserName(),myProps.getFtpPassword(),null,myProps.getFtpPath(),fileName,in);

          System.out.println("relsult: " + flag);
          } catch (Exception e) {
              e.printStackTrace();
          }
      } catch (Exception e) {
        e.printStackTrace();
      }

   }


    /**
     * 获取所有的zip文件名
     */
    public   List<String>  getFileNameList(String dirName){
        List<String> fileList = new ArrayList<>();
        try {
            String[] list=new File(dirName).list();
            for(int i = 0;i<list.length;i++){
                if(list[i].contains(".zip")){
                    fileList.add(list[i]);
                   }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return fileList;
    }

}
